<html>
<head>

<title>Groovy Goodness: Use GroovyWS to Access SOAP Web Services (Part 3)</title>

<script language="javascript" src="scripts/shCore.js"></script> 
<script language="javascript" src="scripts/shLegacy.js"></script> 
<script language="javascript" src="scripts/shBrushJava.js"></script> 
<script language="javascript" src="scripts/shBrushXml.js"></script> 
<script language="javascript" src="scripts/shBrushJScript.js"></script> 
<script language="javascript" src="scripts/shBrushGroovy.js"></script> 
<script language="javascript" src="scripts/shBrushPlain.js"></script> 
<script language="javascript" src="scripts/shBrushBash.js"></script> 
 
<link href="styles/reset.css" rel="stylesheet" type="text/css" />
<link href="styles/shCore.css" rel="stylesheet" type="text/css" />
<link type="text/css" rel="stylesheet" href="styles/shThemeRDark.css"/>
<link href="styles/blog.css" rel="stylesheet" type="text/css" />

</head>
<body>

<a href="index.html">Back to index</a>

<h3 class="post-title">Groovy Goodness: Use GroovyWS to Access SOAP Web Services (Part 3)</h3>

<div class="post">
<p>We learned in the previous posts (<a href="http://mrhaki.blogspot.com/2010/11/groovy-goodness-use-groovyws-to-access.html">Part 1</a>, <a href="http://mrhaki.blogspot.com/2010/11/groovy-goodness-use-groovyws-to-access_14.html">Part 2</a>) how to create a Grails SOAP web service and how to access it with GroovyWS. In this post we learn how we can apply logging interceptors to our client. The output of these logging interceptors is the XML send and received from the SOAP web service. This can be very useful for tracing the messages or for debugging.</p>
<p>The <code>WSClient</code> class of GroovyWS has a property named <code>client</code> of type <code>org.apache.cxf.endpoint.Client</code>. This is our entry point to the Apache CXF client support. We need a way to get a reference to the <code>client</code> property so we can apply logging interceptors for the input and output. Unfortunately the <code>WSClient</code> API has not a method to get a reference to the property. But we write out client code in Groovy so we can use Groovy's MetaClass support to write out own get method to return the client property.</p>
<pre class="brush:groovy;highlight:[26,27,28,32,33,34]">
package com.mrhaki.groovyws.client

import groovyx.net.ws.WSClient
import org.apache.cxf.interceptor.LoggingInInterceptor
import org.apache.cxf.interceptor.LoggingOutInterceptor

class BlogWSClient {

    String wsdlUrl

    def proxy

    def findAuthor(String name) {
        createProxy()
        def searchRequest = createSearchRequest(name)
        proxy.findAuthor searchRequest
    }

    private def createSearchRequest(String name) {
        def searchRequest = proxy.create('com.mrhaki.groovyws.server.SearchRequest')
        searchRequest.authorName = name
        searchRequest
    }

    private void createProxy() {
        if (!proxy) {
            WSClient.metaClass.getCxfClient = { ->
                delegate.client
            }
            proxy = new WSClient(wsdlUrl, this.class.classLoader)
            proxy.initialize()

            def cxfClient = proxy.cxfClient
            cxfClient.inInterceptors.add(new LoggingInInterceptor())
            cxfClient.outInterceptors.add(new LoggingOutInterceptor())
        }
    }

}
</pre>
<p>After this change we can run our tests again from the Gradle build file with <code>$ gradle -q test</code>. We can open the generated test result HTML file and look for the System.err output to see the input and output messages:</p>
<pre class="brush:plain;light:true">
Nov 14, 2010 11:00:15 PM org.apache.cxf.interceptor.LoggingOutInterceptor$LoggingCallback onClose
INFO: Outbound Message
---------------------------
ID: 1
Address: http://localhost:8080/server/services/blog
Encoding: UTF-8
Content-Type: text/xml
Headers: {SOAPAction=[""], Accept=[*/*]}
Payload: &lt;soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"&gt;&lt;soap:Body&gt;&lt;ns1:findAuthor xmlns:ns1="http://server.groovyws.mrhaki.com"&gt;&lt;in0 xmlns="http://server.groovyws.mrhaki.com"&gt;&lt;authorName&gt;mrhaki&lt;/authorName&gt;&lt;/in0&gt;&lt;/ns1:findAuthor&gt;&lt;/soap:Body&gt;&lt;/soap:Envelope&gt;
--------------------------------------
Nov 14, 2010 11:00:15 PM org.apache.cxf.interceptor.LoggingInInterceptor logging
INFO: Inbound Message
----------------------------
ID: 1
Response-Code: 200
Encoding: UTF-8
Content-Type: text/xml;charset=UTF-8
Headers: {content-type=[text/xml;charset=UTF-8], Date=[Sun, 14 Nov 2010 22:00:15 GMT], transfer-encoding=[chunked], Server=[Apache-Coyote/1.1]}
Payload: &lt;soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;&lt;soap:Body&gt;&lt;ns1:findAuthorResponse xmlns:ns1="http://server.groovyws.mrhaki.com"&gt;&lt;ns1:out&gt;&lt;blogItems xmlns="http://server.groovyws.mrhaki.com"&gt;&lt;BlogItem&gt;&lt;id&gt;2&lt;/id&gt;&lt;text&gt;Sample blogitem one.&lt;/text&gt;&lt;title&gt;Title1&lt;/title&gt;&lt;version&gt;0&lt;/version&gt;&lt;/BlogItem&gt;&lt;BlogItem&gt;&lt;id&gt;1&lt;/id&gt;&lt;text&gt;Sample blogitem two.&lt;/text&gt;&lt;title&gt;Title2&lt;/title&gt;&lt;version&gt;0&lt;/version&gt;&lt;/BlogItem&gt;&lt;/blogItems&gt;&lt;id xmlns="http://server.groovyws.mrhaki.com"&gt;1&lt;/id&gt;&lt;name xmlns="http://server.groovyws.mrhaki.com"&gt;mrhaki&lt;/name&gt;&lt;version xmlns="http://server.groovyws.mrhaki.com"&gt;0&lt;/version&gt;&lt;/ns1:out&gt;&lt;/ns1:findAuthorResponse&gt;&lt;/soap:Body&gt;&lt;/soap:Envelope&gt;
--------------------------------------
</pre
</div>

<script language="javascript"> 
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.config.clipboardSwf = 'scripts/clipboard.swf';
SyntaxHighlighter.defaults['first-line'] = 0;
SyntaxHighlighter.defaults['auto-links'] = false;
SyntaxHighlighter.all();
dp.SyntaxHighlighter.HighlightAll('code');
</script>

</body>
</html>